package com.demodis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demodis.dao.ModelDaoI;
import com.demodis.dao.impl.ModelDaoImpl;
import com.demodis.domain.TblModel;
import com.demodis.domain.TblUser;
import com.demodis.service.ModelServiceI;

public class ModelServiceImpl implements ModelServiceI {

	ModelDaoI modelDao = new ModelDaoImpl();
	
	@Override
	public Object getTreeModel(TblUser user) {
		boolean falg = true;
		if(!"admin".equals(user.getUsername())) {
			falg = false;
		}
		
		List<TblModel> list = modelDao.getModel(falg);
		List<Object> json = new ArrayList<Object>(); 
		for(TblModel model : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", model.getModId());
			map.put("text", model.getModName());
			map.put("pid", model.getParMod());
			String url = model.getModUrl();
			if(url != null) {
				Map<String, String> attributes = new HashMap<String, String>();
				attributes.put("url", model.getModUrl());
				map.put("attributes", attributes);
			} else {
				map.put("attributes", "");
			}
			json.add(map);
		}
		
		return json;
	}
	
	@Override
	public Object getModelDate() {
		List<TblModel> list = modelDao.getModel(true);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", list.size());
		List<Object> rows = new ArrayList<Object>();
		for(TblModel model : list) {
			Map<String, Object> row = new HashMap<String, Object>();
			row.put("id", model.getModId());
			row.put("text", model.getModName());
			row.put("modDes", model.getModDes());
			row.put("pid", model.getParMod());
			row.put("modCD", model.getModCd());
			row.put("modUrl", model.getModUrl());
			rows.add(row);
		}
		
		map.put("rows", rows);
		
		return rows;
	}

	@Override
	public boolean save(TblModel model) {
		return modelDao.save(model);
	}

	@Override
	public boolean delete(TblModel model) {
		return modelDao.delete(model);
	}

	@Override
	public boolean modify(TblModel model) {
		return modelDao.modify(model);
	}
}
